package com.android.hidephotosplus.adapter;



import java.util.ArrayList;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.android.hidephotosplus.R;
import com.android.hidephotosplus.model.ModelMedia;
import com.android.hidephotosplus.util.Config;
import com.android.hidephotosplus.util.MLoadMedia;
import com.android.hidephotosplus.util.RetainCache;

public class GalleryAdapter extends ArrayAdapter<ModelMedia> {

	private ArrayList<ModelMedia> arrMedias;

	private Context context;

	private ViewHolder viewHolder;

	private int sizeGallery;

	private MLoadMedia loadMedia = null;

	private boolean isPublic;
	
	public GalleryAdapter(Context context, ArrayList<ModelMedia> objects,
			boolean isPublic) {
		super(context, R.layout.item_gallery, objects);

		this.isPublic = isPublic;

		this.context = context;
		this.arrMedias = objects;
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		sizeGallery = display.getWidth() / 4;
		RetainCache c = RetainCache.getOrCreateRetainableCache();
		loadMedia = c.loadMedia;
		if (loadMedia == null) {
			loadMedia = new MLoadMedia(Config.onSize(context),
					Config.MAX_PIXELS_WIDTH, Config.MAX_PIXELS_HEIGHT);
			c.loadMedia = loadMedia;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View view = convertView;
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.item_gallery, parent, false);

			viewHolder = new ViewHolder();
			viewHolder.imageView = (ImageView) view.findViewById(R.id.image);
			viewHolder.imagePlay = (ImageView) view
					.findViewById(R.id.imagePlay);
			view.setTag(viewHolder);
		} else
			viewHolder = (ViewHolder) view.getTag();

		LayoutParams params = view.getLayoutParams();
		if (params != null) {
			params.height = sizeGallery;
			params.width = sizeGallery;
			view.setLayoutParams(params);
		}

		if (position != -1 && position < arrMedias.size()) {
			ModelMedia item = getItem(position);
			if (item != null) {
				if (isPublic)
					loadMedia.loadBitmap(context, item.isVideo,
							item.getPathMediaStock(), viewHolder.imageView,
							false, mNotifyRunnable, isPublic);
				else
					loadMedia.loadBitmap(context, item.isVideo,
							item.getPathMediaHidden(), viewHolder.imageView,
							false, mNotifyRunnable, isPublic);
				if (item.isVideo) {
					viewHolder.imagePlay.setVisibility(View.VISIBLE);
				} else
					viewHolder.imagePlay.setVisibility(View.GONE);
			}
		}
		return view;
	}

	private class ViewHolder {
		ImageView imageView;
		ImageView imagePlay;
	}

	Runnable mNotifyRunnable = new Runnable() {
		@Override
		public void run() {
			notifyDataSetChanged();
		}
	};

}
